package com.chaoxin.lovehelp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaoxin.lovehelp.dao.CategoryDao;
import com.chaoxin.lovehelp.dao.FellowDao;
import com.chaoxin.lovehelp.entity.CategoryEntity;
import com.chaoxin.lovehelp.entity.FellowEntity;
import com.chaoxin.lovehelp.common.enums.SystemEnum;
import com.chaoxin.lovehelp.common.enums.vo.activityRecipientVo;
import com.chaoxin.lovehelp.common.enums.vo.allRecipientVo;
import com.chaoxin.lovehelp.common.enums.vo.recipientSelectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaoxin.lovehelp.common.utils.PageUtils;

import com.chaoxin.lovehelp.dao.RecipientDao;
import com.chaoxin.lovehelp.entity.RecipientEntity;
import com.chaoxin.lovehelp.service.RecipientService;

import javax.servlet.http.HttpServletRequest;


@Service("recipientService")
public class RecipientServiceImpl extends ServiceImpl<RecipientDao, RecipientEntity> implements RecipientService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private FellowDao fellowDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params, HttpServletRequest request) {

        List<allRecipientVo> vos = getRecipientsList(params,request);
        //分页处理
        Integer pageIndex = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());
        int from = (pageIndex - 1) * limit;
        int to = from + limit > vos.size() ? vos.size() : from + limit;
        List<allRecipientVo> vos1 = new ArrayList<>(vos.subList(from, to));
        IPage<allRecipientVo> page = new Page<>(pageIndex, limit);
        page.setRecords(vos1);
        page.setTotal(vos.size());
        page.setPages(vos.size() % limit > 0 ? vos.size() / limit + 1 : vos.size() / limit);
        return new PageUtils(page);
    }
    //获取某个类别的受助者人数
    @Override
    public Integer getPersonsNum(Integer id) {
        QueryWrapper<RecipientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_display",1).eq("category_id",id);
        int num = 0;
        if(wrapper!=null) {
            num = (int) count(wrapper);
        }
        return num;
    }

    @Override
    public void removeIds(List<Integer> list) {
        if(list!=null){
            for (Integer id: list) {
                RecipientEntity recipient = this.baseMapper.selectById(id);
                recipient.setIsDisplay(0);
                this.baseMapper.updateById(recipient);
            }
        }
    }
    //获取某个会员对接的受助者人数
    @Override
    public int getHelpNum(Integer id) {
        QueryWrapper<RecipientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_display",1).eq("fellow_id",id);
        int num = 0;
        if(wrapper!=null){
            num = (int) count(wrapper);
        }
        return num;
    }

    //获取某个会员对接的所有受助者，（废弃）
    @Override
    public List<allRecipientVo> getHelpPeople(HttpServletRequest request, Integer id) {
        //登录用户
        FellowEntity loginUser =(FellowEntity) request.getSession().getAttribute(SystemEnum.LOGIN_STATUS.getName());
        //非管理员只能查自己的名下受助者，管理员无限制
        if (loginUser.getRole()!=1 && loginUser.getId()!=2 && loginUser.getId()!=id) {
            return new ArrayList<>();
        }

        QueryWrapper<RecipientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_display",1).eq("fellow_id",id);
        List<RecipientEntity> list = this.list(wrapper);
        List<allRecipientVo> vos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            allRecipientVo vo = new allRecipientVo();
            BeanUtils.copyProperties(list.get(i),vo);
            CategoryEntity category = categoryDao.selectById(vo.getCategoryId());
            vo.setCategoryName(category.getName());
            vos.add(vo);
        }
        return vos;
    }

    //根据id返回受助者信息
    @Override
    public activityRecipientVo getRecipentInfos(Integer id) {
        RecipientEntity recipient = this.getById(id);
        activityRecipientVo vo = new activityRecipientVo();
        BeanUtils.copyProperties(recipient,vo);
        CategoryEntity category = categoryDao.selectById(vo.getCategoryId());
        vo.setCategoryName(category.getName());
        FellowEntity fellow = fellowDao.selectById(vo.getFellowId());
        if (fellow != null) {
            vo.setFellowName(fellow.getName());
            vo.setFellowTel(fellow.getTel());
        }

        return vo;
    }
    //获取单个受助者信息
    @Override
    public allRecipientVo getRecipientVo(Integer id) {
        RecipientEntity recipient = this.getById(id);
        allRecipientVo vo = new allRecipientVo();
        BeanUtils.copyProperties(recipient,vo);
        if(recipient.getFellowId()!=null){
            FellowEntity fellow = fellowDao.selectById(recipient.getFellowId());
            vo.setFellowTel(fellow.getTel());
            vo.setFellowName(fellow.getName());
        }
        CategoryEntity category = categoryDao.selectById(recipient.getCategoryId());
        vo.setCategoryName(category.getName());
        return vo;
    }
    //初始化受助者下拉框，管理员获取所有受助者，普通用户获得名下受助者
    @Override
    public List<recipientSelectVo> getAllRecipient(HttpServletRequest request) {
        FellowEntity fellowEntity =(FellowEntity) request.getSession().getAttribute(SystemEnum.LOGIN_STATUS.getName());
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("is_display",1);
        if (fellowEntity.getRole()!=1 && fellowEntity.getRole()!=2) {
            wrapper.eq("fellow_id",fellowEntity.getId());
        }
        List<RecipientEntity> list = this.list(wrapper);
        List<recipientSelectVo> vos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            recipientSelectVo vo = new recipientSelectVo();
            BeanUtils.copyProperties(list.get(i),vo);
            CategoryEntity category = categoryDao.selectById(vo.getCategoryId());
            vo.setCategoryName(category.getName());
            vos.add(vo);
        }
        return vos;
    }

    //获取受助者信息列表
    public List<allRecipientVo> getRecipientsList(Map<String, Object> params,HttpServletRequest request){
        QueryWrapper<RecipientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_display",1);
        String name = (String)params.get("name");
        String categoryId = (String)params.get("categoryId");
        String startAge = (String)params.get("startAge");
        String endAge = (String)params.get("endAge");
        String nurseGrade = (String)params.get("nurseGrade");
        String address = (String) params.get("address");
        String isLove = (String)params.get("isLove");
        String isMaterial = (String)params.get("isMaterial");
        String fellowName = (String) params.get("fellowName");

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(startAge)){
            wrapper.ge("age",Integer.parseInt(startAge));
        }
        if(!StringUtils.isEmpty(endAge)){
            wrapper.le("age",endAge);
        }
        if(!StringUtils.isEmpty(categoryId)){
            wrapper.eq("category_id",Integer.parseInt(categoryId));
        }
        if(!StringUtils.isEmpty(nurseGrade)){
            wrapper.eq("nurse_grade",nurseGrade);
        }
        if(!StringUtils.isEmpty(address)){
            wrapper.like("address",address);
        }

        if(!StringUtils.isEmpty(isLove)){
            wrapper.eq("is_love",isLove);
        }
        if(!StringUtils.isEmpty(isMaterial)){
            wrapper.eq("is_material",isMaterial);
        }
        //当前登录用户
        FellowEntity loginUser =(FellowEntity) request.getSession().getAttribute(SystemEnum.LOGIN_STATUS.getName());
        if (loginUser.getRole()!=1&& loginUser.getRole()!=2){
            wrapper.eq("fellow_id",loginUser.getId());
        }
        List<RecipientEntity> recipients = this.list(wrapper);
        List<allRecipientVo> vos = new ArrayList<>();
        for (int i = 0; i < recipients.size(); i++) {
            RecipientEntity item = recipients.get(i);
            allRecipientVo vo = new allRecipientVo();

            if(item.getFellowId()!=null){
                FellowEntity fellow = fellowDao.selectById(item.getFellowId());
                if(!StringUtils.isEmpty(fellowName)){
                    if(fellow.getName().indexOf(fellowName)<0){
                        System.out.println(2);
                        continue;
                    }
                }
                vo.setFellowName(fellow.getName());
                vo.setFellowTel(fellow.getTel());
            }
            BeanUtils.copyProperties(item,vo);
            CategoryEntity category = categoryDao.selectById(item.getCategoryId());
            vo.setCategoryName(category.getName());
            vos.add(vo);

        }
        return vos;
    }

}